<template>
  <div>
    <!-- 添加按钮、模糊搜索、查询按钮 -->
    <div class="search">
      <el-form
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
        size="mini"
      >
        <el-button @click="toAdd" class="btn" type="primary" size="mini"
          >添加</el-button
        >

        <el-form-item>
          <el-input
            clearable
            v-model="searchForm.username"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            clearable
            v-model="searchForm.status"
            placeholder="请选择状态"
          >
            <el-option label="禁用" value="禁用"></el-option>
            <el-option label="正常" value="正常"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="toSearch" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        style="width: 100%"
        size="mini"
      >
        <el-table-column
          prop="id"
          width="50"
          align="center"
          label="编号"
        ></el-table-column>
        <el-table-column align="center" label="用户名">
          <template slot-scope="scope">
            <span v-if="scope.row.username === null">--</span>
            <span v-else>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="姓名">
          <template slot-scope="scope">
            <span v-if="scope.row.realname === null">--</span>
            <span v-else>{{ scope.row.realname }}</span>
          </template></el-table-column
        >
        <el-table-column align="center" label="手机号">
          <template slot-scope="scope">
            <span v-if="scope.row.telephone === null">--</span>
            <span v-else>{{ scope.row.telephone }}</span>
          </template></el-table-column
        >
        <el-table-column prop="gender" align="center" label="性别">
          <template slot-scope="scope">
            <div v-if="scope.row.gender">
              <i
                v-if="scope.row.gender === 'male'"
                class="el-icon-male green_info"
              />
              <i v-else class="el-icon-female red_info" />
            </div>
            <div v-else>暂无</div>
          </template>
        </el-table-column>
        <el-table-column width="150" align="center" label="状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
              >{{ scope.row.status }}</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              class="warning_info"
              type="text"
              icon="el-icon-edit-outline"
              size="mini"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              class="red_info"
              type="text"
              icon="el-icon-delete"
              size="mini"
              @click="toDelete(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="text"
              icon="el-icon-document"
              size="mini"
              @click="toDetails(scope.row.id)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-div">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20, 30]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 添加模态框 对话框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form size="medium" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          prop="username"
          label="用户名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="realname"
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.realname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="telephone"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.telephone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="gender" label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="form.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑模态框 对话框 -->
    <!-- :before-close="toClose" -->
    <el-dialog :title="title" :visible.sync="visible2" @close="toClose">
      <el-form size="medium" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          prop="username"
          label="用户名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="realname"
          label="姓名"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.realname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="telephone"
          label="手机号"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.telephone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model.number="form.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="gender" label="性别" :label-width="formLabelWidth">
          <el-radio-group v-model="form.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="status" label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio label="禁用">禁用</el-radio>
            <el-radio label="正常">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible2 = false">取 消</el-button>
        <el-button type="primary" @click="toSave2">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageQuery,
  saveOrUpdate,
  addUserWidthRole,
  deleteById,
} from "@/api/user";

import { showFileURL, uploadFileURL } from "@/utils/config";

export default {
  data() {
    return {
      showFileURL,
      uploadFileURL,
      categoryData: [],
      searchForm: {
        username: "",
        status: "",
      },
      // 表格展示的数据
      tableData: [],
      // 分页参数
      params: {
        page: 1,
        pageSize: 10,
        roleId: 1,
      },
      // 总条数
      total: 0,
      // 加载
      loading: false,
      //添加模态框的显示与隐藏
      visible: false,
      //编辑模态框的显示与隐藏
      visible2: false,
      // 表单数据
      form: {
        // roleId:"6",
      },
      // 表单控件标题的宽度
      formLabelWidth: "80px",
      // 模态框的标题
      title: "",
      //男女生选择
      radio: "1",
      // 校验规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "change" },
        ],
        realname: [
          { required: true, message: "请输入姓名", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
      },
    };
  },
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.queryData();
      },
      deep: true,
    },
  },

  computed: {},
  methods: {
    //// 获取员工数据
    async queryWaiterData() {
      let res = await pageQuery({ page: 1, pageSize: 100, roleId: 1 });
      this.categoryData = res.data.list;
    },
    // 分页模糊获取员工数据
    async queryData() {
      this.loading = true;
      let temp = {
        ...this.params,
        ...this.searchForm,
      };
      // 遍历对象
      for (let key in temp) {
        if (!temp[key]) {
          delete temp[key];
        }
      }

      let res = await pageQuery(temp);
      // 加载完毕
      this.loading = false;
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    // 查询按钮点击事件处理程序
    toSearch() {
      this.queryData();
    },
    // 每页显示的条数发生变化时的事件处理程序
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params = {
        page: 1,
        pageSize: val,
      };
    },
    // 当前页码发生变化时的事件处理程序
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.page = val;
    },
    // 添加按钮事件处理程序
    toAdd() {
      this.queryWaiterData();
      // 清空表单数据
      this.form = {};
      this.title = "添加员工信息";
      this.form.roleId = "1";
      this.visible = true;
    },
    //添加模态框的保存
    toSave() {
      // 获取dom的值，对值进行校验 form.name
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 校验通过，做自己的业务，保存
          let res = await addUserWidthRole(this.form);
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: "成功",
            message: res.message,
          });
          this.queryData();
          this.visible = false;
        } else {
          // 校验不通过
          return false;
        }
      });
    },
    //编辑模态框的保存
    toSave2() {
      // 获取dom的值，对值进行校验 form.name
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 校验通过，做自己的业务，保存
          let res = await saveOrUpdate(this.form);
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: "成功",
            message: res.message,
          });
          this.queryData();
          this.visible2 = false;
        } else {
          // 校验不通过
          return false;
        }
      });
    },
    // 关闭模态框
    toClose() {
      // 重置表单验证
      this.$refs.ruleForm.resetFields();
    },
    // 编辑按钮的事件处理程序
    toEdit(row) {
      this.queryWaiterData();
      // 设置表单数据
      this.form = { ...row };
      this.title = "编辑员工信息";
      this.visible2 = true;
    },
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteById({ id });
          this.$notify.success({
            title: "成功",
            message: res.success,
          });
          this.queryData();
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消删除",
          });
        });
    },
    //跳转到个人详情页
    toDetails(id) {
      this.$router.push({
        path: "/admin/waiter/Details",
        query: { id },
      });
    },
  },
  created() {
    this.queryWaiterData();
    this.queryData();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
/* scss less css预处理器 */
.search {
  display: flex;
  // height: 40px;
  .btn {
    margin-right: 10px;
  }
}
</style>